import {
  Injectable,
  NestInterceptor,
  ExecutionContext,
  CallHandler,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

class ApiResponse<T> {
  constructor(
    public success: boolean,
    public code: number,
    public message: string,
    public data: T,
    public timestamp: string,
  ) {}
}

@Injectable()
export class ApiResponseInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    return next.handle().pipe(
      map((data) => {
        return new ApiResponse(true, 200, 'OK', data, new Date().toISOString());
      }),
    );
  }
}
